package com.wzdigit.wms.basic.service.util;

import com.wzdigit.wms.basic.domain.dtos.inventory.UpdateInventoryInputDto;
import com.wzdigit.wms.basic.domain.inventory.Goods;
import com.wzdigit.wms.basic.enums.AsnOpFlagEnum;
import com.wzdigit.wms.basic.service.code.InventoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 库存更新工具类
 * Created by zyf on 2021/9/8 10:28
 */
@Slf4j
@Service
public class InvUtilService {
    @Autowired
    private InventoryService inventoryService;

    /**
     * 增加库存
     *使用场景：入库等
     * @param addGoodsList  新增的SN
     * @param opNo          操作源单号
     * @param opCode        操作代码
     * @param asnOpFlag     新增/更新标识
     * @return
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void addInv(List<Goods> addGoodsList,String opNo,String opCode,String loginName,String asnOpFlag) throws Exception{
        log.info("-----------"+opNo+"增加库存-----------");
        if (asnOpFlag==null){
            asnOpFlag=AsnOpFlagEnum.INSERT.code;
        }
        Map<String, List<Goods>> goodsMap = addGoodsList.stream().collect(Collectors.groupingBy(t ->
                (t.getWhsId() + "#" + t.getLocId() + "#" + t.getOwner() + "#" +
                        t.getSkuId() + "#" + t.getFactory() + "#" + t.getVendorId() + "#" +
                        t.getLotno() + "#" + t.getInvAttr() + "#" + t.getQaStatus() + "#"), Collectors.toList()));
        for (String goodsKey : goodsMap.keySet()) {
            List<Goods> goodsInvList = goodsMap.get(goodsKey);
            Goods goodsInv = goodsInvList.get(0);
            BigDecimal goodsQty = goodsInvList.stream().map(t -> t.getQty()).reduce(BigDecimal.ZERO, BigDecimal::add);

            UpdateInventoryInputDto input = new UpdateInventoryInputDto();
            input.setWhsId(goodsInv.getWhsId());			// 仓库
            input.setOwner(goodsInv.getOwner());			// 货主
            input.setSkuId(goodsInv.getSkuId());			// 物料
            input.setFactory(goodsInv.getFactory());		// 组织
            input.setVendorId(goodsInv.getVendorId());		// 供应商
            input.setInvAttr(goodsInv.getInvAttr());		// 库存性质
            input.setQaStatus(goodsInv.getQaStatus());		// QA状态
            input.setUnit(goodsInv.getUnit());				// 单位
            input.setQty(goodsQty);
            input.setOperator(loginName);
            inventoryService.updateLocInventroy(input);
            inventoryService.updateZoneInventory(input);
        }

        if (asnOpFlag.equals(AsnOpFlagEnum.INSERT.code)){
            log.info("-----------"+opNo+"新增SN-----------");
        }else if (asnOpFlag.equals(AsnOpFlagEnum.UPDATE.code)){
            log.info("-----------"+opNo+"更新SN-----------");
        }


    }

    /**
     * 减少库存
     *使用场景：出库等
     * @param subGoodsList  减少的SN
     * @param opNo          操作源单号
     * @param opCode        操作代码
     * @param alocFlag      出库分配标识
     * @return
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void subInv(List<Goods> subGoodsList,String opNo,String opCode,Boolean alocFlag,String loginName) throws Exception{

    }

    /**
     * 先减后加
     *使用场景：拣货，调拨，上架等
     * @param subGoodsList  减少的SN
     * @param addGoodsList  新增的SN
     * @param opNo          操作源单号
     * @param opCode        操作代码
     * @param alocFlag      拣货分配标识
     * @return
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void moveInv(List<Goods> subGoodsList,List<Goods> addGoodsList,String opNo,String opCode,Boolean alocFlag,String loginName) throws Exception{

    }


}
